home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / poly_area.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  73 lines

  1. ; $Id: poly_area.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1984-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. Function Poly_area,x,y, SIGNED=signed
  7. ;+
  8. ; NAME:
  9. ;    POLY_AREA
  10. ;
  11. ; PURPOSE:
  12. ;    Return the area of a polygon given the coordinates
  13. ;    of its vertices.
  14. ;
  15. ; CATEGORY:
  16. ;    Analytical Geometry
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;    Result = POLY_AREA(X, Y)
  20. ;
  21. ; INPUTS:
  22. ;    It is assumed that the polygon has N vertices with N sides
  23. ;    and the edges connect the vertices in the order:
  24. ;
  25. ;    [(x1,y1), (x2,y2), ..., (xn,yn), (x1,y1)].
  26. ;
  27. ;    i.e. the last vertex is    connected to the first vertex.
  28. ;
  29. ;    X:    An N-element vector of X coordinate locations for the vertices.
  30. ;
  31. ;    Y:    An N-element vector of Y coordinate locations for the vertices.
  32. ;
  33. ; Keyword Inputs:
  34. ;    SIGNED = If set, returned a signed area. Polygons with edges
  35. ;    listed in counterclockwise order have a positive area, while those
  36. ;    traversed in the clockwise direction have a negative area.
  37. ; OUTPUTS:
  38. ;    POLY_AREA returns the area of the polygon.  This value is always 
  39. ;    positive.
  40. ;
  41. ; COMMON BLOCKS:
  42. ;    None.
  43. ;
  44. ; SIDE EFFECTS:
  45. ;    None.
  46. ;
  47. ; RESTRICTIONS:
  48. ;    None.
  49. ;
  50. ; PROCEDURE:
  51. ;    The area is computed as:
  52. ;        Area =     1/2 * [ x1y2 + x2y3 + x3y4 +...+x(n-1)yn + xny1 
  53. ;            - y1x2 - y2x3 -...-y(n-1)xn - ynx1)
  54. ;
  55. ; MODIFICATION HISTORY:
  56. ;    DMS, July, 1984.
  57. ;    DMS, Aug, 1996, Added SIGNED keyword.
  58. ;-
  59. on_error,2                      ;Return to caller if an error occurs
  60. n = n_elements(x)
  61. if (n le 2) then message, 'Not enough vertices'
  62. if n ne n_elements(y) then message,'X and Y arrays must have same size'
  63.  
  64. ; Check type of arithmetic result, be sure we do things in floating.
  65. if (size(x[0] + y[0]))[1] lt 4 then begin
  66.     xx=float(x)        ;Be sure its floating
  67.     a = total(xx*shift(y,-1) - y*shift(xx,-1))/2. ;This is it.
  68. endif else a = total(x*shift(y,-1) - y*shift(x,-1))/2.  ;Already floating
  69.  
  70. if keyword_set(signed) then return, a else return, abs(a)
  71. end
  72.  
  73.